package net.alche.sdd.fatis;

import java.sql.Connection;
public class SddDataSourceConnection {
  Connection cnn = null;
  SddDataSource pool = null;
  long useMs = 0;
  boolean isClosed = true;
  boolean isUseFlag = false;

  public SddDataSourceConnection(SddDataSource pl, Connection cnn) {
    this.pool = pl;
    this.cnn = cnn;
  }

  public boolean isClose() {
    return isClosed;
  }

  public boolean isUse() {
    return isUseFlag;
  }

  public void setUse(boolean isUse) {
    this.isUseFlag = isUse;
  }

  public boolean isTimeOut(long now, long diff) {
    return now - useMs > diff;
  }

  public void destroy() {
    try {
      if (cnn != null && !cnn.isClosed()) {
        cnn.close();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    cnn = null;
    isClosed = true;
    isUseFlag = false;
  }

  // 归还到连接池
  public void sendBackPool() {
    isUseFlag = false;
  }

  public Connection getConn() {
    useMs = System.currentTimeMillis();
    return cnn;
  }

  public void setConn(Connection conn) {
    this.cnn = conn;
    isClosed = false;
    useMs = System.currentTimeMillis();
  }
}
